package com.lyf.baoxian.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.lyf.baoxian.R;
import com.lyf.baoxian.adapter.MyGridViewAdapter;
import com.lyf.baoxian.utils.MyMethod;

import java.util.ArrayList;
import java.util.List;

import static com.lyf.baoxian.utils.MyMethod.setColor;

/**
 * Created by 刘亚飞 on 2017/1/19.
 */

public class SelectBaoXianActivity extends Activity implements View.OnClickListener {
    //显示已选保险
    private GridView gv_delete;
    //所有保险
    private GridView gv_add;
    private ViewGroup titleAll;

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    private TextView tv_show;

    private MyGridViewAdapter adapter;
    private List<String> datas;

    private MyGridViewAdapter adapter1;
    private List<String> datas1;

    private boolean isshow = false;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_baoxian);
        initView();
        initData();
    }

    private void initData() {
        MyMethod.setTitle(this, "选择保险");
        datas = new ArrayList<>();
        datas.add("全部");
        datas.add("水稻保险");
        datas.add("玉米保险");
        datas.add("棉花保险");
        datas.add("板栗保险");

        datas1 = new ArrayList<>();
        datas1.add("苹果保险");
        datas1.add("鸭梨保险");
        datas1.add("香蕉保险");
        datas1.add("葡萄保险");
        datas1.add("橘子保险");
        datas1.add("草莓保险");
        datas1.add("水蜜桃保险");

        //以显示的保险
        adapter = new MyGridViewAdapter(SelectBaoXianActivity.this);
        adapter.setDatas(datas);
        gv_delete.setAdapter(adapter);

        //所有保险
        adapter1 = new MyGridViewAdapter(SelectBaoXianActivity.this);
        adapter1.setDatas(datas1);
        gv_add.setAdapter(adapter1);
    }

    private void initView() {
        gv_delete = (GridView) findViewById(R.id.gv_delete);
        gv_add = (GridView) findViewById(R.id.gv_add);
        titleAll = (ViewGroup) findViewById(R.id.title_all);
        tv1 = (TextView) titleAll.findViewById(R.id.tv1);
        tv2 = (TextView) titleAll.findViewById(R.id.tv2);
        tv3 = (TextView) titleAll.findViewById(R.id.tv3);
        tv4 = (TextView) titleAll.findViewById(R.id.tv4);
        tv_show = (TextView) findViewById(R.id.tv_show);

        setColor(tv1,tv2,tv3,tv4);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        gv_delete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == 0) {
                    datas.clear();
                    adapter.setDatas(datas);
                    tv_show.setVisibility(View.VISIBLE);
                    Toast.makeText(SelectBaoXianActivity.this,"删除全部",Toast.LENGTH_SHORT).show();
                }else {
                    datas.remove(i);
                    adapter.setDatas(datas);
                    if (datas.size() == 1 && datas.get(0) != null) {
                        datas.clear();
                        adapter.setDatas(datas);
                        tv_show.setVisibility(View.VISIBLE);
                    }
                }

            }
        });

        gv_add.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String baoxian = datas1.get(i);
                if (datas.contains(baoxian)) {
                    Toast.makeText(SelectBaoXianActivity.this,"您以添加过该保险",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (datas == null) {
                    tv_show.setVisibility(View.GONE);
                }
                datas.add(baoxian);
                adapter.setDatas(datas);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv1:
                setColor(tv1,tv2,tv3,tv4);
                datas1.clear();
                datas1.add("苹果保险");
                datas1.add("鸭梨保险");
                datas1.add("香蕉保险");
                datas1.add("葡萄保险");
                datas1.add("橘子保险");
                datas1.add("草莓保险");
                datas1.add("水蜜桃保险");
                adapter1.setDatas(datas1);
                break;
            case R.id.tv2:
                setColor(tv2,tv1,tv3,tv4);
                datas1.clear();
                datas1.add("黄瓜保险");
                datas1.add("白菜保险");
                datas1.add("生菜保险");
                datas1.add("芹菜保险");
                datas1.add("大葱保险");
                adapter1.setDatas(datas1);
                break;
            case R.id.tv3:
                setColor(tv3,tv1,tv2,tv4);
                datas1.clear();
                datas1.add("小麦保险");
                datas1.add("水稻保险");
                datas1.add("玉米保险");
                adapter1.setDatas(datas1);
                break;
            case R.id.tv4:
                setColor(tv4,tv1,tv2,tv3);datas1.clear();
                datas1.add("测试1保险");
                datas1.add("测试2保险");
                datas1.add("测试3保险");
                adapter1.setDatas(datas1);
                break;
        }
    }


}
